<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/headeradmin.php");
?>

<section>
<hgroup>
    <h1>Gebouw bewerken</h1>
</hgroup>
<?php if(!isset($_POST['Submit'])) {
    
    // A check if the id is still a numeric character and hasn't been changed manually
    if (!is_numeric($_GET['id'])) { 
        header("Location: ".$cfg['basepath']."controller/gebouw/gebouw.php");
    }
    
    // Retrieves the data of the selected building
    $getBuilding = GetBuildingById($_GET['id']);
    while ($buildingRow = mysql_fetch_array($getBuilding))
    {
        $id = $buildingRow['@id'];
        $street = $buildingRow['@street'];
        $number = $buildingRow['@number'];
        $cityid = $buildingRow['@cityid'];
        $year = $buildingRow['@yearofconstruction'];
        $type = $buildingRow['@buildingtypeid'];
    }

    
    ?>
<form  name="registerform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <table>
        <tr>
            <td>Straat</td>
            <td><input required placeholder="Straat" name="straat" value="<?php echo $street; ?>" type="text"></td>
        </tr>
        <tr>
            <td>Huisnummer</td>
            <td><input required placeholder="Huisnummer" value="<?php echo $number; ?>" name="huisnummer" type="text"></td>
        </tr>
        <tr>
            <td>Postcode</td>
            <td>
                <select name="postcode" required>
                <?php
                    // Gets all the ZIP-codes and cities from the databases and selects the one that corresponds with the ZIP-code of the selected building
                    $Cities = GetAllCityOrderByZIP();
                    while($cityRow = mysql_fetch_array($Cities))
                    {
                        if ($cityRow['Id'] == $cityid)
                        {
                            echo "<option selected value=\"".$cityRow['Id']."\">".$cityRow['ZIP']." (".$cityRow['City'].")</option>"; 
                        }
                        else
                        {
                            echo "<option value=\"".$cityRow['Id']."\">".$cityRow['ZIP']." (".$cityRow['City'].")</option>";                             
                        }
                    }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>Bouwjaar</td>
            <td>
                <select name="bouwjaar" required>
                <?php
                for ($i = $curYear = date('Y'); $i>= 1930; $i--)
                {
                    if ($i == $year)
                    {
                        echo "<option selected value=\"".$i."\">".$i."</option>";
                    }
                    else
                    {
                        echo "<option value=\"".$i."\">".$i."</option>";
                    }
                }
                ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>Type</td>
            <td>
                <select name="type" required>
                <?php
                    // Gets all the building-types from the databases and selects the one that corresponds with the building-type of the selected building
                    $BuildingTypes = GetAllBuildingtype();
                    while($buildingtypeRow = mysql_fetch_array($BuildingTypes))
                    {
                        if ($buildingtypeRow['Id'] == $type)
                        {
                            echo "<option selected value=\"".$buildingtypeRow['Id']."\">".$buildingtypeRow['Name']."</option>";
                        }
                        else
                        {
                            echo "<option value=\"".$buildingtypeRow['Id']."\">".$buildingtypeRow['Name']."</option>";
                        }
                    }
                ?>
                </select>
            </td>
        </tr>
        <?php
            // Gets all the building details from the databases and shows the text of the building details that are known for the selected building
            $BuildingSpecs = GetAllBuildingSpecs();
            while($buildingspecRow = mysql_fetch_array($BuildingSpecs))
            {
                echo "<tr><td>".$buildingspecRow['BuildingKey']."</td>";

                $BuildingDetail = GetBuildingDetail($id, $buildingspecRow['Id']);
                $num_rows_buildingdetail = mysql_num_rows($BuildingDetail);
                
                if ($num_rows_buildingdetail == 0)
                {
                    echo "<td><input placeholder=\"".$buildingspecRow['Comment']."\" name=\"".$buildingspecRow['Id']."\" type=\"text\"></td>";
                }
                else
                {
                    while($buildingdetailRow = mysql_fetch_array($BuildingDetail))
                    {
                        echo "<td><input placeholder=\"".$buildingspecRow['Comment']."\" value=\"".$buildingdetailRow['Value']."\" name=\"".$buildingspecRow['Id']."\" type=\"text\"></td>";
                    }
                }
            }

        ?>
 	<tr>
            <td><input type="submit" name="Submit" value="Aanpassen">
            <input type="hidden" name="gebouwid" value="<?php echo $id ?>" />
            <input type="hidden" name="search" value="<?php if (isset($_GET['search'])){ echo $_GET['search']; } else { echo "0"; }  ?>" />
            <input type="hidden" name="start" value="<?php if (isset($_GET['start'])){ echo $_GET['start']; } else { echo 0; }  ?>" /></td>
            <td><input type="button" value="Terug" onclick="window.location='gebouw.php';" /></td>
        </tr>
</table>
</form>
    <?php 
} 
else {
    $delete = array();
    
    // Update the basic data of a building
    $theDate = date('Y-m-d', strtotime($_POST['bouwjaar']."-1-1"));
    UpdateBuilding($_POST['gebouwid'], $_POST['straat'], $_POST['huisnummer'], $_POST['postcode'], $theDate, $_POST['type']);
    
    // Checks of the detail-fields that are entered contain any new fields and adds them
    // The old fields that remain entered are updated
    // The old fields that aren't entered anymore get saved into an array
    $BuildingSpecs = GetAllBuildingSpecs();
    while($buildingspecRow = mysql_fetch_array($BuildingSpecs))
    {
        $BuildingDetail = GetBuildingDetail($_POST['gebouwid'], $buildingspecRow['Id']);
        $num_rows_buildingdetail = mysql_num_rows($BuildingDetail);
        if ($num_rows_buildingdetail == 0)
        {
            if (isset($_POST[$buildingspecRow['Id']]) && $_POST[$buildingspecRow['Id']] != "")
            {
                InsertBuildingDetail($_POST['gebouwid'], $buildingspecRow['Id'], $_POST[$buildingspecRow['Id']]);
            }
        }
        else
        {
            while($buildingdetailRow = mysql_fetch_array($BuildingDetail))
            {
                if ($_POST[$buildingspecRow['Id']] != "")
                {
                    UpdateBuildingDetail($buildingdetailRow['Id'], $_POST[$buildingspecRow['Id']]);
                }
                else
                {
                    $delete[] = $buildingdetailRow['Id'];
                }
            }
        }
    }

    // Every detail-field that is in this array gets removed from the database
    $number_deletes = sizeof($delete);
    if($number_deletes != 0) { // There has to be atleast 1 item in the array
        for($i = 0; $i < $number_deletes; $i++) {
            DeleteBuildingDetail($delete[$i]);
        }
    }
    echo "Het gebouw is aangepast.<br>";
    if ($_POST['search'] == "0")
    {
        echo "Klik <a href=\"gebouw.php?start=".$_POST['start']."\">hier</a> om terug te gaan naar het overzicht.";
    }
    else
    {
        echo "Klik <a href=\"gebouw.php\">hier</a> om terug te gaan naar het overzicht.";
    }
    
    

}
?>
</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>